;; emacs中文使用的一些配置，包括翻译，中文输入等

;; 中文输入法
(require-package 'pyim)
(require-package 'pyim-basedict)
(require 'pyim)
(require 'pyim-basedict)
(setq default-input-method "pyim")
(setq default-input-scheme "quanpin")
;(require-package 'pyim-cangjiedict)
;(require 'pyim-cangjiedict)
;(setq default-input-scheme "cangjiedict")

;; 添加以下函数是为了解决<no-record> is undefined的bug
;; https://github.com/tumashu/pyim/issues/402
(defun pyim-add-unread-command-events (key &optional reset)
  (when reset
    (setq unread-command-events nil))
  (setq unread-command-events
        (if (characterp key)
            (cons key unread-command-events)
          (append (mapcan (lambda (e) (list e))
                          (append key nil))
                  unread-command-events))))

;; 设置输入法候选词数目和弹出候选框类型
(setq pyim-page-length 9
	  pyim-page-tooltip 'popup)
;; 配置编程模式时只有注释中可以输入中文
;;(setq-default pyim-english-input-switch-functions
;;			  '(pyim-probe-program-mode))
;(require-package 'pyim-basedict)
(pyim-basedict-enable)
(global-set-key (kbd "C-\\") 'toggle-input-method)

;; 使用有道的翻译
;; (require 'youdao)
;; 以下是设置有道API使用的key，在有道官网申请
;; (setf youdao-key-from "emacsuser")
;; (setf youdao-key "1956092468")

;; 有道翻译
(require-package 'youdao-dictionary)
;;(defun after-init-hook-youdao-dictionary ()
  "进入有道字典查询模式后执行的回调"
  ;; TODO 待添加取消evil-mode中按钮q,p,y绑定
;;  (message "youdao-dictionary"))
;;(add-hook 'youdao-dictionary-mode 'after-init-hook-dictionary-mode)

;; 设置词典
;; 由于目前没有windows版本的sdcv.exe
(if (eq system-type 'gnu/linux)
;; 字典使用插件
;; 使用字典需要安装sdcv软件，同时需要安装字典资源(在~/.emacs.d/dic中)
;; 安装sdcv软件后，创建链接文件~/.stardict/dic -> ~/.emacs.d/dic。
;; 配置方式参照chinese-yas
    (progn
      (require-package 'chinese-yasdcv)
      (setq yasdcv-sdcv-dicts
            '(("langdaohy" "朗道汉英字典5.0" "langdao" t)
              ("langdaoyh" "朗道英汉字典5.0" "langdao" t)
              ("bcgm" "本草纲目" nil t)
              ("xhzd" "新华字典" nil t)
              ("jsjch" "计算机词汇" nil t)
              ("lanconghy" "懒虫简明汉英词典" nil t)
              ("lancongyh" "懒虫简明英汉词典" nil t)))))

(defun lugg/yasdcv-translate ()
  "翻译光标处的单词，如果翻译buffer已经存在，执行删除buffer操作"
  (interactive)
  (let ((out-buffer nil))
	(setq out-buffer (get-buffer "*Stardict Output*"))
	(if out-buffer
		(progn
		  (if (not (string= "*Stardict Output*" (buffer-name)))
			  (switch-to-buffer (get-buffer "*Stardict Output*")))
		  (kill-buffer-and-window))
	  (progn
		(yasdcv-translate-at-point)
		(if (get-buffer "*Stardict Output*")
			(switch-to-buffer-other-window (get-buffer "*Stardict Output*")))))))

;;字体设置
(defun set-font (english chinese english-size chinese-size)
  (set-face-attribute 'default nil :font
                      (format   "%s:pixelsize=%d"  english english-size))
  (dolist (charset '(kana han symbol cjk-misc bopomofo))
    (set-fontset-font (frame-parameter nil 'font) charset
                      (font-spec :family chinese :size chinese-size))))
(if (eq system-type 'windows-nt)
	(set-font   "Consolas" "Hiragino Sans GB" 13 16))

(provide 'init-chinese)
